<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="难度" prop="difficulty">
        <el-select v-model="queryParams.difficulty" placeholder="请选择难度" clearable>
          <el-option
              v-for="dict in aeoj_problem_difficulty"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['management:problem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['management:problem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['management:problem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['management:problem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="problemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column label="难度" align="center" prop="difficulty">
        <template #default="scope">
          <dict-tag :options="aeoj_problem_difficulty" :value="scope.row.difficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="总提交数" align="center" prop="submissionCount" />
      <el-table-column label="总通过数" align="center" prop="acceptedCount" />
      <el-table-column label="总点赞数" align="center" prop="likes" />
      <el-table-column label="总收藏数" align="center" prop="favorites" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['management:problem:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['management:problem:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改题目对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="problemRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="输入用例" prop="inputCases">
          <el-input v-model="form.inputCases" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="输出用例" prop="outputCases">
          <el-input v-model="form.outputCases" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="form.tags" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="难度" prop="difficulty">
          <el-radio-group v-model="form.difficulty">
            <el-radio
                v-for="dict in aeoj_problem_difficulty"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提示" prop="hints">
          <el-input v-model="form.hints" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="时间限制(ms)" prop="timeLimit">
          <el-input v-model="form.timeLimit" placeholder="请输入时间限制(ms)" />
        </el-form-item>
        <el-form-item label="内存限制(MB)" prop="memoryLimit">
          <el-input v-model="form.memoryLimit" placeholder="请输入内存限制(MB)" />
        </el-form-item>
        <el-form-item label="总提交数" prop="submissionCount">
          <el-input v-model="form.submissionCount" placeholder="请输入总提交数" />
        </el-form-item>
        <el-form-item label="总通过数" prop="acceptedCount">
          <el-input v-model="form.acceptedCount" placeholder="请输入总通过数" />
        </el-form-item>
        <el-form-item label="总点赞数" prop="likes">
          <el-input v-model="form.likes" placeholder="请输入总点赞数" />
        </el-form-item>
        <el-form-item label="总收藏数" prop="favorites">
          <el-input v-model="form.favorites" placeholder="请输入总收藏数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Problem">
import { listProblem, getProblem, delProblem, addProblem, updateProblem } from "@/api/management/problem";

const {proxy} = getCurrentInstance();
const {aeoj_problem_difficulty} = proxy.useDict('aeoj_problem_difficulty');

const problemList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    tags: null,
    difficulty: null,
  },
  rules: {
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ],
    content: [
      {required: true, message: "内容不能为空", trigger: "blur"}
    ],
    inputCases: [
      {required: true, message: "输入用例不能为空", trigger: "blur"}
    ],
    outputCases: [
      {required: true, message: "输出用例不能为空", trigger: "blur"}
    ],
    difficulty: [
      {required: true, message: "难度不能为空", trigger: "change"}
    ],
    timeLimit: [
      {required: true, message: "时间限制(ms)不能为空", trigger: "blur"}
    ],
    memoryLimit: [
      {required: true, message: "内存限制(MB)不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询题目列表 */
function getList() {
  loading.value = true;
  listProblem(queryParams.value).then(response => {
    problemList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    inputCases: null,
    outputCases: null,
    tags: null,
    difficulty: null,
    hints: null,
    timeLimit: null,
    memoryLimit: null,
    submissionCount: null,
    acceptedCount: null,
    likes: null,
    favorites: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    deleted: null
  };
  proxy.resetForm("problemRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加题目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getProblem(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改题目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["problemRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateProblem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProblem(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除题目编号为"' + _ids + '"的数据项？').then(function () {
    return delProblem(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('management/problem/export', {
    ...queryParams.value
  }, `problem_${new Date().getTime()}.xlsx`)
}

getList();
</script>
